#include "TCHAR.H"

void replace(String_t& subject, LPCTSTR search, LPCTSTR newText, size_t pos)
{
  if(pos == std::string::npos)
    return;

  pos = subject.find(search, pos);
  if(pos != std::string::npos)
    subject.replace(pos, _tcslen(search), newText);
  
  pos += _tcslen(search);
  replace(subject, search, newText, pos);
}

void replaceTextXML( String_t& context, size_t pos )
{
  //if(pos == std::string::npos)
  //  return;

  //pos = context.find(_T('&'), pos);
  //if(pos != std::string::npos)
  //{
  //  context.replace(pos, 1, _T("&amp;"));
  //  pos += 5;
  //  replaceTextXML(context, pos);
  //}

  //pos = context.find(_T('<'), pos);
  //if(pos != std::string::npos)
  //{
  //  context.replace(pos, 1, _T("&lt;"));
  //  pos += 4; 
  //  replaceTextXML(context, pos);
  //}

  //pos = context.find(_T('>'), pos);
  //if(pos != std::string::npos)
  //{
  //  context.replace(pos, 1, _T("&gt;"));
  //  pos += 4;
  //  replaceTextXML(context, pos);
  //}

  //pos = context.find(_T('"'), pos);
  //if(pos != std::string::npos)
  //{
  //  context.replace(pos, 1, _T("&quot;"));
  //  pos += 6;
  //  replaceTextXML(context, pos);
  //}
  
  for(int index = 0; index < (int)context.length(); index++)
  {
    TCHAR ch = context[index];
    switch(ch)
    {
    case _T('&'): context.replace(index, 1, _T("&amp;")); index += 4; break;
    case _T('<'): context.replace(index, 1, _T("&lt;")); index += 3; break;
    case _T('>'): context.replace(index, 1, _T("&gt;")); index += 3; break;
    case _T('"'): context.replace(index, 1, _T("&quot;")); index += 5; break;
    }
  }

 /* String_t::iterator ch = context.begin();
  while(ch != context.end())
  {
    switch(*ch)
    {
      case _T('&'): context.replace(pos, 1, _T("&amp;")); pos += 4; ch += 4; break;
      case _T('<'): context.replace(pos, 1, _T("&lt;")); pos += 3; ch += 3; break;
      case _T('>'): context.replace(pos, 1, _T("&gt;")); pos += 3; ch += 3; break;
      case _T('"'): context.replace(pos, 1, _T("&quot;")); pos += 5; ch += 5; break;
    }
    pos++;
    ch++;
  }*/
}
